package com.hncboy.chatgpt.front.controller;

import com.alipay.api.AlipayApiException;
import com.google.common.collect.Maps;
import com.google.zxing.WriterException;
import com.hncboy.chatgpt.base.annotation.FrontPreAuth;
import com.hncboy.chatgpt.base.util.LocalJsonUtil;
import com.hncboy.chatgpt.front.service.PayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Iterator;
import java.util.Map;

@FrontPreAuth
@AllArgsConstructor
@Tag(name = "网站支付相关工具接口")
@RestController
@RequestMapping
public class PayController {

    private final PayService payService;

    /**
     * 1、用户点击去支付后，pc端把相应的参数传输后端，
     * 2、组建订单信息，获取支付信息，添加订单，返回给pc端
     */

    /**
     * 支付宝相关接口
     */
    @Operation(summary = "支付宝电脑网站下单接口")
    @PostMapping("/ali/pcPay")
    public void aliPcPay(Map<String, Object> paramMap) throws AlipayApiException {
        //校验订单相关参数，比如如果有金额，判断金额是否为负数，是否被篡改，通过后获取支付链接
        payService.aliPcPay(paramMap);
    }

    @Operation(summary = "支付宝电脑网站异步通知接口")
    @PostMapping(value = "/ali/pcPay/notify")
    public String aliPcPayNotify(HttpServletRequest request){
        try {
            return payService.aliPcPayNotify(parseRequst(request));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "failure";
    }

    /**
     * 微信相关接口
     */
    @Operation(summary = "微信Native下单接口，返回二维码给前端")
    @PostMapping("/wx/native")
    public void wxNative(Map<String, Object> paramMap, HttpServletResponse response) throws IOException, WriterException {
        //校验订单相关参数，比如如果有金额，判断金额是否为负数，是否被篡改，通过后获取支付链接
        payService.wxNative(paramMap, response);
    }

    @Operation(summary = "微信Native异步通知接口")
    @PostMapping(value = "/wx/native/notify")
    public ResponseEntity<String> wxNativeNotify(HttpServletRequest request){
        try {
            Map<String, String> result = payService.wxNativeNotify(parseRequst(request));
            return new ResponseEntity<>(LocalJsonUtil.ObjToString(result), HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Map<String, String> resp = Maps.newHashMap();
        resp.put("code", "FAIL");
        resp.put("message", "失败");
        return new ResponseEntity<>(LocalJsonUtil.ObjToString(resp), HttpStatus.OK);
    }

    /**
     * request转字符串
     */
    public static Map<String, String> parseRequst(HttpServletRequest request) {
        Map<String, String[]> requestParams = request.getParameterMap();
        Map<String, String> params = Maps.newHashMap();
        for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) {
            String name = iter.next();
            String[] values = requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
            }
            params.put(name, valueStr);
        }
        return params;
    }

}
